package com.ady.util;

import com.ady.webmgic.util.PropertiesCacheUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.log4j.Logger;

import java.util.*;

/**
 * <b>description</b>：工具 <br>
 * <b>time</b>：2018-06-22 14:10 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class FrameUtil {

    private static Logger logger = Logger.getLogger(FrameUtil.class);

    /**
     * 创建arraylist
     *
     * @param args
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List newArrayList(Object... args) {
        ArrayList list = new ArrayList();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                list.add(args[i]);
            }
        }
        return list;
    }

    /**
     * 创建hashMap
     *
     * @param args
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map newHashMap(Object... args) {
        HashMap<Object, Object> paramMap = new HashMap<Object, Object>();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                paramMap.put(args[i], args[++i]);
            }
        }
        return paramMap;
    }

    /**
     * 创建LinkedHashMap
     *
     * @param args
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map newLinkedHashMap(Object... args) {
        LinkedHashMap<Object, Object> paramMap = new LinkedHashMap<Object, Object>();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                paramMap.put(args[i], args[++i]);
            }
        }
        return paramMap;
    }

    public static String json(Object object) {
        return json(object, false);
    }

    public static String json(Object object, boolean format) {
        if (format) {
            return JSON.toJSONString(object, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat);
        } else {
            return JSON.toJSONString(object, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
        }
    }

    /**
     * 将数组转换成list
     *
     * @param objs
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List converArrayToList(Object... objs) {
        List list = null;
        if (objs != null) {
            list = new ArrayList();
            for (Object object : objs) {
                list.add(object);
            }
        }
        return list;
    }


    /**
     * 获取志愿文件中key对应的值
     *
     * @param propertiesFileName
     * @param key
     * @param param
     * @return
     */

    public static String getResourceByFile(String propertiesFileName, String key, Object... param) {
        return getResource(propertiesFileName, key, converArrayToList(param));
    }

    /**
     * 获取资源文件中key对应的值
     *
     * @param propertiesFileName
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String getResource(String propertiesFileName, String key, List param) {
        String value = "";
        try {
            Object obj = PropertiesCacheUtil.getValue(propertiesFileName, key);
            if (obj != null) {
                value = obj.toString();
                if (param != null) {
                    for (int i = 0; i < param.size(); i++) {
                        Object val = param.get(i);
                        if (val != null) {
                            String reg = "\\{" + i + "\\}";
                            value = value.replaceAll(reg, val.toString());
                        }
                    }
                }
            } else {
                value = key;
            }
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
        return value;
    }
}
